package com.yhb.controller;

import com.yhb.controller.vo.ArrayWrapperVo;
import com.yhb.controller.vo.PayInfoVo;
import com.yhb.model.Order;
import com.yhb.model.User;
import com.yhb.service.GoodService;
import com.yhb.service.OrderService;
import lombok.var;
import me.edzh.spring.bootstrap.annotations.CurrentUser;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.controller.BaseController;
import me.edzh.spring.bootstrap.model.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {


    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    @NeedAuth
    public Response<Order> createOrder(@RequestBody Order order, @CurrentUser User user) {
        order.setStatus(Order.Status.COMMITTED);
        if (order.getGoodCount() == null || order.getGoodCount() <= 0) { // 非法值过滤
            order.setGoodCount(1L);
        }
        order.setUserId(user.getId());

        return Response.ok(orderService.create(order));
    }

    @GetMapping("/{orderId}/purchase")
    @NeedAuth
    public Response<PayInfoVo> purchase(@PathVariable("orderId") String orderId, @CurrentUser User user) {
        return Response.ok(PayInfoVo.builder().payInfo(orderService.purchaseOrder(orderId, user.getId())).build());
    }

    @GetMapping("/my")
    @NeedAuth
    public Response<ArrayWrapperVo<Order>> myOrder(@CurrentUser User user) {
        return Response.ok(ArrayWrapperVo.<Order>builder().fuckingList(orderService.userOrders(user.getId())).build());
    }

    @GetMapping("/{orderId}")
    @NeedAuth
    public Response<Order> myOrder(@CurrentUser User user, @PathVariable("orderId")String orderId) {
        return Response.ok(orderService.getById(orderId, user.getId()));
    }



}
